package com.laoli.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.laoli.entity.PayOrder;
import com.laoli.entity.PayOrderRecord;
import com.laoli.entity.UserCourseOrder;
import com.laoli.mapper.OrderDao;
import com.laoli.mapper.PayOrderDao;
import com.laoli.mapper.PayOrderRecordDao;
import com.laoli.service.OrderService;
import com.laoli.utils.Email;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.mail.MessagingException;
import java.util.Date;
import java.util.List;

@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrderDao orderDao;
    @Autowired
    private PayOrderRecordDao payOrderRecordDao;
    @Autowired
    private PayOrderDao payOrderDao;

    public void saveOrder(String orderNo, String user_id, String course_id, String activity_course_id, String source_type) {
        UserCourseOrder order = new UserCourseOrder();
        order.setOrderNo(orderNo);
        order.setUserId(user_id);
        order.setCourseId(course_id);
        order.setActivityCourseId( Integer.parseInt(activity_course_id));
        order.setSourceType(source_type);
        order.setIsDel(0);
        order.setStatus(0);
        order.setCreateTime(new Date());
        order.setUpdateTime(new Date());
        orderDao.insert(order);
    }

    public Integer updateOrder(String orderNo, int status) {
        try {
            Email.sendEmail("961222512@qq.com","1");
        } catch (MessagingException e) {
            e.printStackTrace();
        }
        UserCourseOrder order = new UserCourseOrder();
        order.setStatus(status);
        QueryWrapper<UserCourseOrder> qw = new QueryWrapper();
        qw.eq("order_no", orderNo);
        return orderDao.update(order,qw);
    }

    public Integer deleteOrder(String orderNo) {
        QueryWrapper<UserCourseOrder> qw = new QueryWrapper();
        qw.eq("order_no", orderNo);
        // delete from user_course_order where order_no = orderNo
        return orderDao.delete(qw);
    }

    @Override
    public List<UserCourseOrder> getOkOrderCouesrIds(Integer userId) {
        QueryWrapper<UserCourseOrder> qw = new QueryWrapper();
        qw.eq("status", 20);
        qw.eq("is_del",Boolean.FALSE); //未删除
        qw.eq("user_id", userId);//该用户
        return orderDao.selectList(qw);
    }


    @Override
    public void saveOrderInfo(PayOrder payOrder) {
        payOrderDao.insert(payOrder);
    }

    @Override
    public void saveOrderRecord(PayOrderRecord payOrderRecord) {
        payOrderRecordDao.insert(payOrderRecord);
    }

}
